<?php
/**
 * Short Description
 *
 * Long Description
 *
 * @category
 * @package
 * @subpackage
 * @copyright
 * @author     Sandeep Manne<sandeep.manne@costrategix.com>
 */

class Core_DAO extends Zend_Db_Table_Abstract
{
	/**
	 * @var string name of the table
	 */
	protected $_name;

	/**
	 * @var string primary key of the table
	 */
	protected $_primary;

	/**
	 * Overides parent constructor to throw an exeption
	 *
	 * @return unknown_type
	 */
	public function __construct()
	{
		try {
			parent::__construct();
			$this->_setupMetadata();
		} catch (Exception $e) {
			throw $e;
		}
	}

	/**
	 * Used to start secure transactions
	 */
	public function startTransaction()
	{
		return $this->getDefaultAdapter()->beginTransaction();
	}

	/**
	 * Used to end transactions securely
	 */
	public function endTransaction()
	{
		return $this->getDefaultAdapter()->commit();
	}

	/**
	 * Used to rollback transaction if terminated
	 */
	public function terminateTransaction()
	{
		return $this->getDefaultAdapter()->rollBack();
	}

	/**
	 * (non-PHPdoc)
	 * @see Zend/Db/Table/Zend_Db_Table_Abstract#insert()
	 */
	public function insert(array $data)
	{
		$data = array_intersect_key($data, $this->_metadata);
		$data['created_date_time'] = date('Y-m-d H:i:s');
		return parent::insert($data);
	}

	/**
	 * (non-PHPdoc)
	 * @see Zend/Db/Table/Zend_Db_Table_Abstract#update()
	 */
	public function update($data, $where)
	{
		$data = array_intersect_key($data, $this->_metadata);
		return parent::update($data, $where);
	}
	/**
	 * (non-PHPdoc)
	 * @see Zend/Db/Table/Zend_Db_Table_Abstract#update()
	 */
	public function updateData($data, $where)
	{
		$data = array_intersect_key($data, $this->_metadata);
		return parent::update($data, $where);
	}
	/**
	 * return columns in a table
	 *
	 * @return unknown_type
	 */
	public function getColumns()
	{
		return $this->_getCols();
	}

	/**
	 * Return row(s) where key = value
	 *
	 * @param unknown_type $key
	 * @param unknown_type $value
	 */
	public function getDataByKeyValue($key, $value)
	{
		return $this->select()
		->from($this->_name)
		->where($key." = ?", $value)
		->query()
		->fetchAll();
	}

	/**
	 * Returns all data of a table
	 */
	public function getCollection($limit = null)
	{
		//TODO Implement Limit
		return  $this->select()
		->from($this->_name)
		->query()
		->fetchAll();
	}
	
	public function generateXml($select = null)
	{
		if ($select && !($select instanceof Zend_Db_Select)) {
			throw new Exception("Generate xml needs object of type Zend_Db_Select");
		}
		if ($select) $resultSet = $select->query()->fetchAll();
		else 		 $resultSet = $this->getCollection();
		$xml = new SimpleXMLElement('<?xml version = "1.0" encoding = "UTF-8"?><'.$this->_name.'/>');
		$i = 1;
		foreach ($resultSet as $result) {
			$row = $xml->addChild('row');
			$row->addAttribute('id', $i++);
			foreach ($result as $key => $value) {
				$row->addChild($key, $value);
			}
		}
		return $xml->asXML();
	}
	
	public function generateCsv($select = null)
	{
		if ($select && !($select instanceof Zend_Db_Select)) {
			throw new Exception("Generate xml needs object of type Zend_Db_Select");
		}
		$csv = "";
		if ($select) $resultSet = $select->query()->fetchAll();
		else 		 $resultSet = $this->getCollection();
		$keys = array_keys($resultSet[0]);
		foreach ($keys as $key) {
			$csv .= $key . ",";
		} 
		foreach ($resultSet as $result) {
			foreach ($result as $key => $value) {
				$csv .= $value . ","; 
			}
		}
		
		return $csv;
	}
}
